/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author williamprado
 */
public class navigationServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String target = request.getParameter("target");
        HttpSession session = request.getSession();

        if(target==null){
            session.setAttribute("error", "Lo sentimos. Ha ocurrido un error procesando la solicitud.");
            response.sendRedirect("error.jsp");
        }else if(target.equals("searchFlights")){
            List<VOs.Country> clist = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                clist = bd.getAllCountries();
                int a1 = 1;
                int a2 = 1;
                session.setAttribute("countryList", clist);
                session.removeAttribute("cityList1");
                session.removeAttribute("cityList2");
                session.setAttribute("selectedCountry1", a1);
                session.setAttribute("selectedCountry2", a2);
                response.sendRedirect("buscarVuelos.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("getCitiesR")){
            String cou = "";
            int a = 0;
            List<VOs.City> clist = null;
            List<VOs.Country> countr = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                cou = (String) request.getParameter("country");
                countr = bd.getAllCountries();
                for(int i=0;i<countr.size();i++){
                    int p = Integer.parseInt(""+countr.get(i).getId());
                    if(countr.get(i).getName().equals(cou)){
                        a = p;
                    }
                }
                clist = bd.getCitiesByCountry(cou);
                session.setAttribute("countryList", countr);
                session.setAttribute("cityList", clist);
                session.removeAttribute("restaurantList");
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarRestaurantes.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("getCitiesT")){
            String cou = "";
            int a = 0;
            List<VOs.City> clist = null;
            List<VOs.Country> countr = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                cou = (String) request.getParameter("country");
                countr = bd.getAllCountries();
                for(int i=0;i<countr.size();i++){
                    int p = Integer.parseInt(""+countr.get(i).getId());
                    if(countr.get(i).getName().equals(cou)){
                        a = p;
                    }
                }
                clist = bd.getCitiesByCountry(cou);
                session.setAttribute("countryList", countr);
                session.setAttribute("cityList", clist);
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarTeatro.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("getCitiesH")){
            String cou = "";
            int a = 0;
            List<VOs.City> clist = null;
            List<VOs.Country> countr = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                cou = (String) request.getParameter("country");
                countr = bd.getAllCountries();
                for(int i=0;i<countr.size();i++){
                    int p = Integer.parseInt(""+countr.get(i).getId());
                    if(countr.get(i).getName().equals(cou)){
                        a = p;
                    }
                }
                clist = bd.getCitiesByCountry(cou);
                session.setAttribute("countryList", countr);
                session.setAttribute("cityList", clist);
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarHoteles.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("getCities")){
            String cou1 = "";
            String cou2 = "";
            int a1 = 0;
            int a2 = 0;
            List<VOs.City> clist1 = null;
            List<VOs.City> clist2 = null;
            List<VOs.Country> countr = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                cou1 = (String) request.getParameter("country1");
                cou2 = (String) request.getParameter("country2");
                System.out.println("pais1 "+cou1);
                System.out.println("pais2 "+cou2);
                countr = bd.getAllCountries();
                for(int i=0;i<countr.size();i++){
                    int p = Integer.parseInt(""+countr.get(i).getId());
                    if(countr.get(i).getName().equals(cou1)){
                        a1 = p;
                    }
                    if(countr.get(i).getName().equals(cou2)){
                        a2 = p;
                    }
                }
                clist1 = bd.getCitiesByCountry(cou1);
                clist2 = bd.getCitiesByCountry(cou2);
                session.setAttribute("countryList", countr);
                session.setAttribute("cityList1", clist1);
                session.setAttribute("cityList2", clist2);
                session.setAttribute("selectedCountry1", a1);
                session.setAttribute("selectedCountry2", a2);
                response.sendRedirect("buscarVuelos.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("getCities1")){
            String cou = "";
            int a = 0;
            List<VOs.City> clist = null;
            List<VOs.Country> countr = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                cou = (String) request.getParameter("country1");
                countr = bd.getAllCountries();
                for(int i=0;i<countr.size();i++){
                    int p = Integer.parseInt(""+countr.get(i).getId());
                    if(countr.get(i).getName().equals(cou)){                        
                        a = p;                        
                    }                    
                }                
                clist = bd.getCitiesByCountry(cou);
                session.setAttribute("countryList", countr);
                session.setAttribute("cityList", clist);
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarVuelos.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("getCities2")){
            String cou = "";
            int a = 0;
            List<VOs.City> clist = null;
            List<VOs.Country> countr = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                cou = (String) request.getParameter("country2");
                countr = bd.getAllCountries();
                for(int i=0;i<countr.size();i++){
                    int p = Integer.parseInt(""+countr.get(i).getId());
                    if(countr.get(i).getName().equals(cou)){
                        a = p;
                    }
                }
                clist = bd.getCitiesByCountry(cou);
                session.setAttribute("countryList", countr);
                session.setAttribute("cityList", clist);
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarVuelos.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("searchHotels")){
            List<VOs.Country> clist = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                int a = 0;
                clist = bd.getAllCountries();
                session.setAttribute("countryList", clist);
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarHoteles.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("searchRestaurants")){
            List<VOs.Country> clist = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                int a = 0;
                clist = bd.getAllCountries();
                session.setAttribute("countryList", clist);
                session.removeAttribute("cityList");
                session.removeAttribute("restaurantList");
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarRestaurantes.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("searchTheaters")){
            List<VOs.Country> clist = null;
            businessDelegate.BusinessDelegate bd = new businessDelegate.BusinessDelegate();
            try{
                int a = 0;
                clist = bd.getAllCountries();
                session.setAttribute("countryList", clist);
                session.removeAttribute("cityList");
                session.removeAttribute("theaterList");
                session.setAttribute("selectedCountry", a);
                response.sendRedirect("buscarTeatro.jsp");
            }catch(Exception e){
                e.printStackTrace();
                response.sendRedirect("index.jsp");
            }
        }else if(target.equals("home")){
            session.removeAttribute("cityList");
            response.sendRedirect("index.jsp");
        }

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet navigationServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet navigationServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
